/*2yNauLMSj1ohw3Ey1zGZ1pOTs1s19u9oWpg/CLG0mmaVLRWx9hosuBDKm+PRNb29*/
/**
 * 多使用部门编辑后事件
 */
import {ajax, toast} from 'nc-lightapp-front';

import fa from 'fa';

const {fa_utils} = fa;

const {metaUtil} = fa_utils;

export default function (props, moduleId, field, newValue, changedrows, index, record, type, method) {
    if ('pk_dept' === field) {
        let currRows = this.usedeptsEditBefore;
        //去除空行
        currRows = currRows.filter((row) =>  row.values.pk_dept.value);

        let emptyRowData = metaUtil.genEmptyRowDataByMeta(props, moduleId);

        let newRows = [];
        let curr_depts = currRows.map((row) => row.values.pk_dept);
        for (let usedept of newValue) {
            let isExisted = curr_depts.some((item) => {
                return item.value === usedept.refpk;
            });
            if (!isExisted) {
                let newRow = JSON.parse(JSON.stringify(emptyRowData));
                newRow.status = 2;
                newRow.values['pk_dept'] = {display: usedept.refcode, scale: -1, value: usedept.refpk, isEdit: false};
                newRow.values['pk_dept.name'] = {display: usedept.refname, scale: -1, value: usedept.refname};
                newRows.push(newRow);
            }
        }
        let clickRow = currRows[index];
        if(clickRow) {
            let clickRowIsChecked = newValue.filter((row) => row.refpk === clickRow.values.pk_dept.value);
            if(!clickRowIsChecked.length) {
                currRows.splice(index, 1);
            }
        }

        let lastEmptyRow = JSON.parse(JSON.stringify(emptyRowData));
        currRows = currRows.concat(newRows).concat([lastEmptyRow]);
        props.cardTable.setTableData(moduleId, {rows: currRows}, null, false);//isCache设置为false，以便取消时回退数据
    }
}
/*2yNauLMSj1ohw3Ey1zGZ1pOTs1s19u9oWpg/CLG0mmaVLRWx9hosuBDKm+PRNb29*/